package com.cango.student;

import java.util.Date;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 多线程，5个线程内部打印hello和word，
 * hello在前，
 * 要求提供一种方法使得5个线程先全部打印出hello后再打印5个word。
 */
public class Test {
    AtomicInteger num = new AtomicInteger(0);

    class newThread implements Runnable {
        @Override
        public void run() {
            while (true) {
                synchronized (num) {
                    if (num.get() == 5) {
                        System.out.println("world");
                        return;
                    } else {
                        System.out.println("hello");
                        try {
                            num.incrementAndGet();
                            num.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }

    public void notifyNowAll() {
        while (true) {
            synchronized (num) {
                if (num.get() == 5) {
                    num.notifyAll();
                    return;
                }
            }
        }
    }

    static int i;
    public static void main(String[] args) {
//        Test t = new Test();
//
//        Thread t1 = new Thread(t.new newThread());
//        Thread t2 = new Thread(t.new newThread());
//        Thread t3 = new Thread(t.new newThread());
//        Thread t4 = new Thread(t.new newThread());
//        Thread t5 = new Thread(t.new newThread());
//        t1.start();
//        t2.start();
//        t3.start();
//        t4.start();
//        t5.start();
//
//        t.notifyNowAll();
    }
}
